package com.qf.main.content.service.impl;

import cn.hutool.core.date.DateUtil;
import com.aliyun.oss.model.ObjectMetadata;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.common.db.utils.CommonBeanutils;
import com.qf.common.oss.utils.FileUtils;
import com.qf.main.content.common.qo.HomePageSlideshowQo;
import com.qf.main.content.common.vo.HomePageSlideshowVo;
import com.qf.main.content.entity.HomePageSlideshow;
import com.qf.main.content.mapper.HomePageSlideshowMapper;
import com.qf.main.content.service.HomePageSlideshowService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

/**
 * 首页轮播图(HomePageSlideshow)表服务实现类
 *
 * @author makejava
 * @since 2024-01-20 10:26:36
 */
@Service
public class HomePageSlideshowServiceImpl  implements HomePageSlideshowService {
    @Resource
    private HomePageSlideshowMapper homePageSlideshowMapper;
    @Resource
    private FileUtils fileUtils;

    @Override
    @Transactional
    public Integer insertHomePageSlideshowService(HomePageSlideshowQo homePageSlideshowQo){
        HomePageSlideshow homePageSlideshow = CommonBeanutils.convertTo(homePageSlideshowQo, HomePageSlideshow::new);
        int insert = homePageSlideshowMapper.insert(homePageSlideshow);
        return insert;
    }

    @Override
    public String uploadImage(MultipartFile multipartFile) {
        String file;
        try {
            file = fileUtils.uploadImage(multipartFile,"home"+"/"+"page");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return file;
    }
    @Override
    public HomePageSlideshow getHomePageSlideshow(String homePageSlideshowName){
        QueryWrapper<HomePageSlideshow> qw = new QueryWrapper();
        qw.eq(HomePageSlideshow.COL_SLIDESHOW_NAME,homePageSlideshowName);
        HomePageSlideshow homePageSlideshow = homePageSlideshowMapper.selectOne(qw);
        return   homePageSlideshow;
    }

    @Override
    public Integer modifyHomePageSlideshow(HomePageSlideshowQo homePageSlideshowQo){
        HomePageSlideshow homePageSlideshow = CommonBeanutils.convertTo(homePageSlideshowQo, HomePageSlideshow::new);

        QueryWrapper<HomePageSlideshow> qw = new QueryWrapper();
        qw.eq(HomePageSlideshow.COL_SLIDESHOW_NAME,homePageSlideshow.getSlideshowName());

        int update = homePageSlideshowMapper.update(homePageSlideshow, qw);
        return update;
    }
    @Override
    public Integer removeHomePageSlideshow(Integer id){

        return homePageSlideshowMapper.deleteById(id);
    }

}

